package com.example.administrator.nurse.widget;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Xfermode;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.ImageView;

/**
 * Created by Administrator on 2016/4/16.
 */
public class CircleImageView extends ImageView {
    private Paint mPaint;
    public CircleImageView(Context context) {
        super(context);
        mPaint=new Paint();
        mPaint.setAntiAlias(true);
    }

    public CircleImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mPaint=new Paint();
        mPaint.setAntiAlias(true);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int width=Math.min(getMeasuredWidth(),getMeasuredHeight());
        setMeasuredDimension(width, width);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        Drawable drawable=getDrawable();
        Bitmap bitmap;
        if(drawable instanceof BitmapDrawable){
            bitmap=((BitmapDrawable) drawable).getBitmap();
        }else {
            bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicWidth(), Bitmap.Config.ARGB_8888);
            Canvas canvas1 = new Canvas(bitmap);
            drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicWidth());
            drawable.draw(canvas1);
        }
        canvas.drawBitmap(toRoundDrawable(bitmap, getWidth()), 0, 0, mPaint);
    }

    public static Bitmap toRoundDrawable(Bitmap bitmap,int circleWidth){
        int width=bitmap.getWidth();
        int height=bitmap.getHeight();
        final float roundPx;
        float left,top,right,bottom,dst_left,dst_top,dst_right,dst_bottom;
        Log.i("height",Integer.toString(circleWidth));
        if (width<=height){
            roundPx=circleWidth/2;
            left=0;
            top=0;
            right=width;
            bottom=width;
            height=width;
            dst_left=0;
            dst_top=0;
            dst_right=circleWidth;
            dst_bottom=circleWidth;
        }else{
            roundPx=circleWidth/2;
            float clip=(width-height)/2;
            left=clip;
            right=width-clip;
            top=0;
            bottom=height;
            width=height;
            dst_left=0;
            dst_top=0;
            dst_right=circleWidth;
            dst_bottom=circleWidth;
        }
        Bitmap output=Bitmap.createBitmap(circleWidth,circleWidth, Bitmap.Config.ARGB_8888);
        Canvas canvas=new Canvas(output);
        final int color=0xff424242;
        final Paint paint=new Paint();
        final Rect src=new Rect((int)left,(int)top,(int)right,(int)bottom);
        final Rect dst=new Rect((int)dst_left,(int)dst_top,(int)dst_right,(int)dst_bottom);
        final RectF rectF=new RectF(dst);
        paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(color);
        canvas.drawCircle(roundPx, roundPx, roundPx, paint);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        canvas.drawBitmap(bitmap, src, dst, paint);
        return output;
    }
}
